/*
 * print_asc.c
 *
 *  Created on: Apr 11, 2011
 *      Author: philip
 */
#include <stdio.h>
#include <string.h>
#include "print_ll.h"

void print_ll_tree(ll *ll){
	if(ll->asc != NULL){
		print_asc(ll->asc);
	}
	if(ll->next != NULL){
		print_ll_tree(ll->next);
	}else{
		fprintf(stderr,"print done\n");
	}
}

void print_asc(asc *asc){
	char *id;
	id = malloc(strlen("%esp"));
	strcpy(id,"%esp");
	switch(asc->kind){
	case LABEL:
		printf("%s\n",asc->par.id);
		break;
	case FUNCLABEL:
		printf("%s\n",asc->par.id);
		break;
	case ALLOC:
		if(asc->par.arguments_const.first>0){
			printf("\taddl  $-%i,%s\n",asc->par.arguments_const.first*4,id);
		}
		break;
	case DEALLOC:
		if(asc->par.arguments_const.first>0){
			printf("\taddl  $%i,%s\n",asc->par.arguments_const.first*4,id);
		}
		break;
	case MOV:
		printf("\tmovl  %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case MOV_CONST:
		printf("\tmovl  $%i,%s\n", asc->par.arguments_const.first, asc->par.arguments_const.second->regid);
		break;
	case PUSH:
		printf("\tpushl %s\n", asc->par.arguments.first->regid);
		break;
	case PUSH_CONST:
		printf("\tpushl $%i\n", asc->par.arguments_const.first);
		break;
	case POP:
		printf("\tpopl  %s\n", asc->par.arguments.first->regid);
		break;
	case ADD:
		printf("\taddl  %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case ADD_CONST:
		printf("\taddl  $%i,%s\n", asc->par.arguments_const.first, asc->par.arguments_const.second->regid);
		break;
	case SUB:
		printf("\tsubl  %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case SUB_CONST:
		printf("\tsubl  $%i,%s\n", asc->par.arguments_const.first, asc->par.arguments_const.second->regid);
		break;
	case DIV:
		printf("\tidiv  %s\n", asc->par.arguments.first->regid);
		break;
	case DIV_CONST:
		break;
	case MUL:
		printf("\timul  %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case MUL_CONST:
		printf("\timul  $%i,%s\n", asc->par.arguments_const.first, asc->par.arguments_const.second->regid);
		break;
	case AND:
		printf("\tand  %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case OR:
		printf("\tor   %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case NEG:
		printf("\tor   %s\n", asc->par.arguments.first->regid);
		break;
	case CMP:
		printf("\tcmp   %s,%s\n", asc->par.arguments.first->regid, asc->par.arguments.second->regid);
		break;
	case CMP_CONST:
		printf("\tcmp   $%i,%s\n", asc->par.arguments_const.first, asc->par.arguments_const.second->regid);
		break;
	case JMP:
		printf("\tjmp   %s\n", asc->par.id);
		break;
	case JG:
		printf("\tjg   %s\n", asc->par.id);
		break;
	case JL:
		printf("\tjl   %s\n", asc->par.id);
		break;
	case JLE:
		printf("\tjle   %s\n", asc->par.id);
		break;
	case JGE:
		printf("\tjge   %s\n", asc->par.id);
		break;
	case JA:
		printf("\tja    %s\n", asc->par.id);
		break;
	case JAE:
		printf("\tjae   %s\n", asc->par.id);
		break;
	case JNE:
		printf("\tjne   %s\n", asc->par.id);
		break;
	case JE:
		printf("\tje    %s\n", asc->par.id);
		break;
	case CALL:
		printf("\tcall  %s\n", asc->par.id);
		break;
	case RETURN:
		printf("%s\n", asc->par.id);
		break;
	case STATICLINK:
		printf("\tpushl %s\n","%ebp");
		break;
	case MOVEPOS:
		if(asc->par.regs.pos1>0){
			printf("\tmovl  %i(%s),",asc->par.regs.pos1,asc->par.regs.first->regid);
		}else{
			printf("\tmovl  %s,",asc->par.regs.first->regid);
		}
		if(asc->par.regs.pos2>0){
			printf("%i(%s)\n",asc->par.regs.pos2,asc->par.regs.second->regid);
		}else{
			printf("%s\n",asc->par.regs.second->regid);
		}
		break;
	}
}
